// pages/add_address/add_address.js
const app = getApp()
const Host = require('../../config').Host
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inputName:'',
        inputPhone:'',
        region:'',
        province:'',
        city: '',
        area: '',
        textArea:'',
        isDefault:false,
    },
    //微信获取收货地址
    chooseAddress: function () {
        var that = this;
        if (wx.chooseAddress) {
            wx.chooseAddress({
                success: function (res) {
                    //console.log(JSON.stringify(res))
                    var data = {
                        //id: res.nationalCode,
                        name: res.userName,
                        phone: res.telNumber,
                        address: res.provinceName + res.cityName + res.countyName,
                        //isDefault: true,
                    };
                    that.setData({
                        inputName: res.userName,
                        inputPhone: res.telNumber,
                        region: res.provinceName + res.cityName + res.countyName,
                        province: res.provinceName,
                        city: res.cityName,
                        area: res.countyName,
                        textArea: res.detailInfo,
                    })
                },
                fail: function (err) {
                    console.log(JSON.stringify(err))
                }
            })
        } else {
            console.log('当前微信版本不支持chooseAddress');
        }
    },
    //选择默认
    chooseDefault:function(){
        var that=this;
        if(that.data.isDefault==false){
            that.setData({
                isDefault:true, 
            })
        }else{
            that.setData({
                isDefault:false,
            })
        }
    },
    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region:e.detail.value[0]+e.detail.value[1]+e.detail.value[2],
            province: e.detail.value[0],
            city: e.detail.value[1],
            area: e.detail.value[2],
        })
    },
    getInputName:function(e){
        this.setData({
            inputName:e.detail.value,
        })
    },
    getInputPhone: function (e) {
        this.setData({
            inputPhone: e.detail.value,
        })
    },
    getTextArea: function (e) {
        this.setData({
            textArea: e.detail.value,
        })
    },
    //保存
    sure:function(){
        var that=this;
        var reg1 = new RegExp(/^[\u4E00-\u9FA5]{2,40}$/);
        var reg2 = new RegExp(/^1(3|4|5|7|8|9)\d{9}$/);
        if(!that.data.inputName){
            wx.showToast({
                title: '请输入姓名',
                icon:'none',
                duration:1500,
            })
        } else if (!reg1.exec(that.data.inputName)){
            wx.showToast({
                title: "姓名格式错误",
                icon: 'none',
                duration: 1500,
            })
        }else if(!that.data.inputPhone){
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 1500,
            })
        }else if(!reg2.exec(that.data.inputPhone)) {
            wx.showToast({
                title: "手机号格式错误",
                icon: 'none',
                duration: 1500,
            })
        }else if (!that.data.region) {
            wx.showToast({
                title: '请选择所在区域',
                icon: 'none',
                duration: 1500,
            })
        }else if(!that.data.textArea) {
            wx.showToast({
                title: '请输入详细地址',
                icon: 'none',
                duration: 1500,
            })
        }else{
          var receiver=that.data.inputName;
          var telphone = that.data.inputPhone;
          var province = that.data.province;
          var city = that.data.city;
          var area = that.data.area;
          var address=that.data.textArea;
          var openid = wx.getStorageSync('openid');
          var defaultAddress = that.data.isDefault;
          wx.request({
            url: Host + 'Order/addressAdd',
            data: { openid, receiver, telphone, province, city, area, address, defaultAddress },
            method: "post",
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              if (res.data.code == 200) {
                wx.showToast({
                  title: '保存成功',
                  icon: 'success',
                  duration: 1500,
                })
                wx.navigateBack({
                  delta: 1,
                })
              } else {
                wx.showToast({
                  title: res.data.errmsg,
                  icon: 'none',
                })
              }
            }
          })
           
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})